﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using HandRise.MBHOP.Controls;
using Handrise.CDCable.Mobile.Business;

namespace Handrise.CDCable.Mobile.UI
{
    public partial class FrmGroundPatrol : FrmBase
    {
        private Plan plan;

        private PlanDeviceService planDeviceService = new PlanDeviceService();

        private bool isGPSConfirm = false;

        private bool isScanConfirm = false;

        private List<PlanDevice> confrimDevices = new List<PlanDevice>();

        private string workType;

        private int index = 0;

        private ScanService scanService;

        public FrmGroundPatrol()
        {
            InitializeComponent();
            ttlbrhead.Text = "设备到位确认";
            ilstbcup.Images.Add(ResourceImage.巡视01);
            ilstbcup.Images.Add(ResourceImage.空白);
            ilstbcup.Images.Add(ResourceImage.返回01);
            ilstdown.Images.Add(ResourceImage.巡视02);
            ilstdown.Images.Add(ResourceImage.空白);
            ilstdown.Images.Add(ResourceImage.返回02);

        }

        private void frmgroundpatrol_Load(object sender, EventArgs e)
        {
            scanService = new ScanService(this);
            scanService.OnReadCode += scanService_OnReadCode;

            ccPlanDeviceDetail.AddMember("DeviceName", "设备名称");
            ccPlanDeviceDetail.AddMember("DeviceType", "设备类型");
            ccPlanDeviceDetail.AddMember("DeviceCode", "设备编码");

            SetButtonState();

            if (IsReturn)
                LoadCacheData();
            else
                LoadData();

            tbPlanTitle.Text = plan.Title;

            //btnGPSConfirm.Enabled = workType == "直埋电缆以及隧道地面巡视";
        }

        private void FrmGroundPatrol_Closed(object sender, EventArgs e)
        {
            scanService.Close();
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        private void LoadData()
        {
            plan = (Plan)Parameter[0];
            workType = (string)Parameter[1];
        }

        /// <summary>
        /// 加载缓存数据
        /// </summary>
        private void LoadCacheData()
        {
            plan = (Plan)Caches[0];
            workType = (string)Caches[1];
        }

        private void SetButtonState()
        {
            pnlPlanDeviceSelect.Visible = confrimDevices.Count > 1;
        }

        void scanService_OnReadCode(string code)
        {
            string deviceCode = code;
            PlanDevice planDevice = planDeviceService.GetPlanDeviceByCode(plan.PlanID, deviceCode);
            if (planDevice == null)
                return;

            isScanConfirm = true;

            ccPlanDeviceDetail.DataSource = planDevice;
            ccPlanDeviceDetail.DataBind();

            //确认到位
            confrimDevices.Clear();
            confrimDevices.Add(planDevice);
            planDeviceService.PlanDeviceConfirmLocation(confrimDevices, "ScanConfirm", 0, 0);

            SetButtonState();


            ////确认是否扫描的设备在之前的通过GPS确认到位的设备中
            //if (confrimDevices != null)
            //{
            //    PlanDevice gpsComfirmPlanDevice = confrimDevices.Where(t => t.DeviceID == planDevice.DeviceID).FirstOrDefault();
            //    if (gpsComfirmPlanDevice == null)
            //        isGPSConfirm = false;
            //}
        }


        private void bcOperate_OnButtonClick(object sender, int buttonIndex)
        {
            switch (buttonIndex)
            {
                case 0:  
                    BeginPatrolDevice();
                    break;
                case 1:
                    break;
                case 2:
                    FormManage.GotoNext("FrmPatrolType", null, null, true);
                    break;

            }
        }

        /// <summary>
        /// 开始巡视设备
        /// </summary>
        private void BeginPatrolDevice()
        {
            if (ccPlanDeviceDetail.DataSource == null)
                return;

            //到位确认
            PlanDevice planDevice = (PlanDevice)(ccPlanDeviceDetail.DataSource);
            //planDeviceService.PlanDeviceComfireLocation(planDevice, isGPSConfirm, isScanConfirm);
            planDevice.ConfirmState = "已确认";

            FormManage.GotoNext("FrmPatrolPoint", new object[] { "FrmGroundPatrol", planDevice, workType }, new object[] { plan, workType }, false);


        }

        private void btnGPSConfirm_Click(object sender, EventArgs e)
        {
            double longitude;
            double latitude;

            confrimDevices = planDeviceService.GetPlanDeviceInGPSRange(plan.PlanID, out longitude, out latitude);

            if (confrimDevices.Count > 0)
            {
                isGPSConfirm = true;

                ccPlanDeviceDetail.DataSource = confrimDevices[0];
                ccPlanDeviceDetail.DataBind();

                planDeviceService.PlanDeviceConfirmLocation(confrimDevices, "GPSConfirm", longitude, latitude);
            }

            SetButtonState();
        }

        private void btnScanComfire_Click(object sender, EventArgs e)
        {
            scanService.Scan();
        }


        private void btnPrior_Click(object sender, EventArgs e)
        {
            index--;
            if (index <= 0)
                index = 0;

            ccPlanDeviceDetail.DataSource = confrimDevices[index];
            ccPlanDeviceDetail.DataBind();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            index++;
            if (index == confrimDevices.Count)
                index = confrimDevices.Count - 1;

            ccPlanDeviceDetail.DataSource = confrimDevices[index];
            ccPlanDeviceDetail.DataBind();
        }



    }
}